package poker;
public class Hand {
    private Card[] cards;
    private int[] value;

    Hand(Deck d)
    {
        boolean kolor=true, straight=false;
        int par=1,par2=1;
        int largeGroupRank=0,smallGroupRank=0;
        int index=0;
        int topStraightValue=0;

        for (int x=0; x<=13; x++)
        {
            ranks[x]=0;
        }
        for (int x=0; x<=4; x++)
        {
            ranks[ cards[x].getRank() ]++;
        }
        for (int x=0; x<4; x++) {
            if ( cards[x].getSuit() != cards[x+1].getSuit() )
                kolor=false;
        }

        for (int x=13; x>=1; x--)
        {
                 if (ranks[x] > par)
                 {
                     if (par != 1)
                     {
                         par2 = par;
                         smallGroupRank = largeGroupRank;
                     }

                     par = ranks[x];
                     largeGroupRank = x;

                 } else if (ranks[x] > par2)
                 {
                     par2 = ranks[x];
                     smallGroupRank = x;
                 }
        }

        if (ranks[1]==1) //w razie wylosowania asa
        {
            orderedRanks[index]=14;
            index++;
        }

        for (int x=13; x>=2; x--)
        {
            if (ranks[x]==1)
            {
                orderedRanks[index]=x; //as
                index++;
            }
        }
        
        for (int x=1; x<=9; x++)
        //brak strita gdy najmniejsza wartosc > 10
        {
            if (ranks[x]==1 && ranks[x+1]==1 && ranks[x+2]==1 && 
                ranks[x+3]==1 && ranks[x+4]==1)
            {
                straight=true;
                topStraightValue=x+4; //4 above bottom value
                break;
            }
        }

        if (ranks[10]==1 && ranks[11]==1 && ranks[12]==1 && 
            ranks[13]==1 && ranks[1]==1) //ace high
        {
            straight=true;
            topStraightValue=14; //higher than king
        }
        
        for (int x=0; x<=5; x++)
        {
            value[x]=0;
        }


        //hand evaluation
        if ( par==1 ) {
            value[0]=1;
            value[1]=orderedRanks[0];
            value[2]=orderedRanks[1];
            value[3]=orderedRanks[2];
            value[4]=orderedRanks[3];
            value[5]=orderedRanks[4];
        }

        if (par==2 && par2==1)
        {
            value[0]=2;
            value[1]=largeGroupRank; //rank pary
            value[2]=orderedRanks[0];
            value[3]=orderedRanks[1];
            value[4]=orderedRanks[2];
        }

        if (par==2 && par2==2) //dwie pary
        {
            value[0]=3;
            //rank wiekszej pary
            value[1]= largeGroupRank>smallGroupRank ? largeGroupRank : smallGroupRank;
            value[2]= largeGroupRank<smallGroupRank ? largeGroupRank : smallGroupRank;
            value[3]=orderedRanks[0];  //+ karta
        }

        if (par==3 && par2!=2)
        {
            value[0]=4;
            value[1]= largeGroupRank;
            value[2]=orderedRanks[0];
            value[3]=orderedRanks[1];
        }

        if (straight && !kolor)
        {
            value[0]=5;
            value[1]=topStraightValue;
        }

        if (kolor && !straight)
        {
            value[0]=6;
            value[1]=orderedRanks[0]; //remis poprzez rank kart
            value[2]=orderedRanks[1];
            value[3]=orderedRanks[2];
            value[4]=orderedRanks[3];
            value[5]=orderedRanks[4];
        }

        if (par==3 && par2==2)
        {
            value[0]=7;
            value[1]=largeGroupRank;
            value[2]=smallGroupRank;
        }

        if (par==4)
        {
            value[0]=8;
            value[1]=largeGroupRank;
            value[2]=orderedRanks[0];
        }

        if (straight && kolor)
        {
            value[0]=9;
            value[1]=topStraightValue;
        }

    }

    void display()
    {
        String s;
        switch( value[0] )
        {

            case 1:
                s="najwyzsza karta";
                break;
            case 2:
                s="para " + Card.rankAsString(value[1]) + "\'s";
                break;
            case 3:
                s="dwie pary " + Card.rankAsString(value[1]) + " " + 
                                Card.rankAsString(value[2]);
                break;
            case 4:
                s="trojka " + Card.rankAsString(value[1]) + "\'s";
                break;
            case 5:
                s=Card.rankAsString(value[1]) + " high straight";
                break;
            case 6:
                s="kolor";
                break;
            case 7:
                s="ful " + Card.rankAsString(value[1]) + " over " + 
                                  Card.rankAsString(value[2]);
                break;
            case 8:
                s="kareta " + Card.rankAsString(value[1]);
                break;
            case 9:
                s="poker " + Card.rankAsString(value[1]) + " high";
                break;
            default:
                s="error pierwsza wartosc posiada zle dane";
        }
        s = "                " + s;
        System.out.println(s);
    }

    void displayAll()
    {
        for (int x=0; x<5; x++)
            System.out.println(cards[x]);
    }

    int compareTo(Hand that)
    {
        for (int x=0; x<6; x++)
        {
            if (this.value[x]>that.value[x])
                return 1;
            else if (this.value[x]<that.value[x])
                return -1;
        }
        return 0; //remis
    }
}